package com.nursinghome.user.controller;

import com.nursinghome.annotation.Log;
import com.nursinghome.bean.RespBean;
import com.nursinghome.bean.RoomOccupancy;
import com.nursinghome.common.service.RoomOccupancyService;
import com.nursinghome.exception.NurisingHomeException;
import com.nursinghome.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/user/occupancy")
public class UserRoomOccController {
    @Autowired
    private RoomOccupancyService roomOccupancyService;
    @GetMapping
    public RespBean selectAll(@RequestHeader(value = "token") String token,
                              @RequestBody RoomOccupancy roomOccupancy) {
        Map<String, Object> map = JwtUtils.parseJwtToMap(token);
        roomOccupancy.setElderlyId(new Long(String.valueOf(map.get("eid"))));
        return RespBean.ok("",roomOccupancyService.selectAll( roomOccupancy));
    }
    //新增预约服务订单
    @Log(name = "房间更换")
    @PostMapping
    public RespBean insert(@RequestHeader(value = "token") String token,
                           @RequestBody RoomOccupancy occupancy) throws NurisingHomeException {
        Map<String, Object> map = JwtUtils.parseJwtToMap(token);
        Long eid = new Long(String.valueOf(map.get("eid")));
        occupancy.setElderlyId(eid);
        occupancy.setStatus(0);
        if (roomOccupancyService.insert(occupancy)) {
            return RespBean.ok("预约成功");
        }
        return RespBean.error("预约失败");
    }
    //修改预约服务订单
    @PutMapping
    public RespBean update(@RequestHeader(value = "token") String token,
                           @RequestBody RoomOccupancy occupancy) throws NurisingHomeException {
        Map<String, Object> map = JwtUtils.parseJwtToMap(token);
        Long eid = new Long(String.valueOf(map.get("eid")));
        occupancy.setElderlyId(eid);
        if (roomOccupancyService.update(occupancy)) {
            return RespBean.ok("预约成功");
        }
        return RespBean.error("预约失败");
    }
    //删除预约服务订单
    @DeleteMapping("/{id}")
    public RespBean delete(@PathVariable("id") Integer id) throws NurisingHomeException {
        if (roomOccupancyService.delete(id)) {
            return RespBean.ok("删除成功");
        }
        return RespBean.error("删除失败");
    }
    //回显预约服务订单数据
    @GetMapping("/{id}")
    public RespBean selectById(@PathVariable("id") Integer id) throws NurisingHomeException {
        RoomOccupancy occupancy = roomOccupancyService.selectById(id);
        return RespBean.ok("",occupancy);
    }
}
